﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Linq;
using System.Collections.ObjectModel;
using System.Device.Location;
using Microsoft.Phone.Tasks;
using WP_Customer_ShopMob.Model;
using Newtonsoft.Json;

namespace WP_Customer_ShopMob.ViewModels
{
    public class ArticleViewModel : BaseViewModel
    {
        private IRepository repo = Data.GetModel;
        private long id;

        public long IdArticle
        {
            get { return id; }
            set { id = value; }
        }

        private string nom;

        public string Nom
        {
            get { return nom; }
            set { nom = value; }
        }

        private string details;

        public string Details
        {
            get { return details; }
            set { details = value; }
        }

        private long idCategorie;

        public long IdCategorie
        {
            get { return idCategorie; }
            set { idCategorie = value; }
        }

        private double prix;

        public double Prix
        {
            get { return prix; }
            set { prix = value; }
        }

        private string urlImage;

        public string UrlImage
        {
            get { return urlImage; }
            set { urlImage = value; }
        }

        private ObservableCollection<DBO_ShopMob.Magasin> listMagasins = new ObservableCollection<DBO_ShopMob.Magasin>();

        public ObservableCollection<DBO_ShopMob.Magasin> ListMagasins
        {
            get { return listMagasins; }
            set { listMagasins = value; }
        }

        private bool isDataLoaded = false;

        public bool IsDataLoaded
        {
            get { return isDataLoaded; }
            set { isDataLoaded = value; }
        }

        public ArticleViewModel()
        {
            
        }

        public void LoadData()
        {
            //list.Add(new DBO_ShopMob.Magasin() { IdMagasin = "1", Ville = "Dammartin en goele" });
            //list.Add(new DBO_ShopMob.Magasin() { IdMagasin = "2", Ville = "paris" });
            //list.Add(new DBO_ShopMob.Magasin() { IdMagasin = "3", Ville = "massy" });
            //list.Add(new DBO_ShopMob.Magasin() { IdMagasin = "4", Ville = "le raincy" });
            //list.Add(new DBO_ShopMob.Magasin() { IdMagasin = "5", Ville = "saint leu la forêt" });
            //list.Add(new DBO_ShopMob.Magasin() { IdMagasin = "6", Ville = "marseille" });
            //list.Add(new DBO_ShopMob.Magasin() { IdMagasin = "7", Ville = "cherbourg" });

            //foreach (var l in list)
            //{
            //    ListMagasins.Add(l);
            //}
            
            repo.GetMagasinForArticle(IdArticle, delegate(string resu, Exception ex)
            {
                var des = JsonConvert.DeserializeObject<List<DBO_ShopMob.Magasin>>(resu);
                foreach (var mag in des)
                {
                    ListMagasins.Add(new DBO_ShopMob.Magasin() { adresse = mag.adresse, cp = mag.cp, ville = mag.ville, idMagasin = mag.idMagasin });
                }
            });
        }
    }
}
